package com.wiscamp.ninechapters.common.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class ParseHelper {
    private ParseHelper() {
    }

    private static final Logger logger = LoggerFactory.getLogger(ParseHelper.class);

    private static final String FAILED_CONVERT_TIP = "Failed to parse the number string.";

    public static Short parseShort(String numberText) {
        try {
            return Short.parseShort(numberText);
        } catch (NumberFormatException e) {
            logger.error("Failed to convert the short integer number.", e);
            return null;
        }
    }

    public static Integer parseInteger(String numberText) {
        try {
            return Integer.parseInt(numberText);
        } catch (NumberFormatException e) {
            logger.error("Failed to convert the integer number.", e);
            return null;
        }
    }

    public static Long parseLong(String numberText) {
        try {
            return Long.parseLong(numberText);
        } catch (NumberFormatException e) {
            logger.error("Failed to convert the long integer number.", e);
            return null;
        }
    }

    public static BigInteger parseBigInteger(String numberText) {
        try {
            return new BigInteger(numberText);
        } catch (NumberFormatException e) {
            logger.error("Failed to convert the big integer number.", e);
            return null;
        }
    }

    public static Float parseFloat(String numberText) {
        try {
            return Float.parseFloat(numberText);
        } catch (NumberFormatException e) {
            logger.error("Failed to convert the float number.", e);
            return null;
        }
    }

    public static Double parseDouble(String numberText) {
        try {
            return Double.parseDouble(numberText);
        } catch (NumberFormatException e) {
            logger.error("Failed to convert the double number.", e);
            return null;
        }
    }

    public static BigDecimal parseBigDecimal(String numberText) {
        try {
            return new BigDecimal(numberText);
        } catch (NumberFormatException e) {
            logger.error("Failed to convert the big decimal number.", e);
            return null;
        }
    }

    public static LocalTime timeOf(int hour, int minute, int second) {
        try {
            return LocalTime.of(hour, minute, second);
        } catch (DateTimeException e) {
            logger.error("Failed to convert the time.", e);
            return null;
        }
    }

    public static LocalTime parseTime(String timeText) {
        try {
            int length = timeText.length();
            String pattern = "HH:mm:ss";
            if (length == 5)
                pattern = "HH:mm";
            return LocalTime.parse(timeText, DateTimeFormatter.ofPattern(pattern));
        } catch (DateTimeException e) {
            logger.error("Failed to convert the time.", e);
            return null;
        }
    }

    public static LocalDate dateOf(int year, int month, int day) {
        try {
            return LocalDate.of(year, month, day);
        } catch (DateTimeException e) {
            logger.error("Failed to convert the date.", e);
            return null;
        }
    }


    /**
     * 文本字符串按间隔符分割成Long列表
     *
     * @param text
     * @param interval
     * @return
     */
    public static Collection<Long> parseLong(String text, String interval) {
        if (text == null || text.isEmpty()) return new ArrayList<>();

        String[] arrays = text.split(interval);
        Collection<Long> list = new ArrayList<>();
        for (var number : arrays) {
            if ("".equals(number)) continue;
            try {
                var element = Long.parseLong(number);
                list.add(element);
            } catch (Exception ex) {
                logger.error(FAILED_CONVERT_TIP, ex);
            }
        }
        return list;
    }

    /**
     * 文本字符串按间隔符分割成Integer列表
     *
     * @param text
     * @param interval
     * @return
     */
    public static Collection<Integer> parseInteger(String text, String interval) {
        if (text == null || text.isEmpty()) return new ArrayList<>();

        String[] arrays = text.split(interval);
        Collection<Integer> list = new ArrayList<>();
        for (var number : arrays) {
            if ("".equals(number)) continue;
            try {
                var element = Integer.parseInt(number);
                list.add(element);
            } catch (Exception ex) {
                logger.error(FAILED_CONVERT_TIP, ex);
            }
        }
        return list;
    }

    public static Collection<BigInteger> parseBigInteger(String text, String interval) {
        if (text == null || text.isEmpty()) return new ArrayList<>();

        String[] arrays = text.split(interval);
        Collection<BigInteger> list = new ArrayList<>();
        for (var number : arrays) {
            if ("".equals(number)) continue;
            try {
                var element = new BigInteger(number);
                list.add(element);
            } catch (Exception ex) {
                logger.error(FAILED_CONVERT_TIP, ex);
            }
        }
        return list;
    }

    /**
     * 文本字符串按间隔符分割成Double列表
     *
     * @param text
     * @param interval
     * @return
     */
    public static Collection<Double> parseDouble(String text, String interval) {
        if (text == null || text.isEmpty()) return new ArrayList<>();

        String[] arrays = text.split(interval);
        Collection<Double> list = new ArrayList<>();
        for (var number : arrays) {
            if ("".equals(number)) continue;
            try {
                var element = Double.parseDouble(number);
                list.add(element);
            } catch (Exception ex) {
                logger.error(FAILED_CONVERT_TIP, ex);
            }
        }
        return list;
    }


    public static Collection<BigDecimal> parseBigDecimal(String text, String interval) {
        if (text == null || text.isEmpty()) return new ArrayList<>();

        String[] arrays = text.split(interval);
        Collection<BigDecimal> list = new ArrayList<>();
        for (var number : arrays) {
            if ("".equals(number)) continue;
            try {
                var element = new BigDecimal(number);
                list.add(element);
            } catch (Exception ex) {
                logger.error(FAILED_CONVERT_TIP, ex);
            }
        }
        return list;
    }

    /**
     * 文本字符串按间隔符分割成String列表
     *
     * @param text
     * @param interval
     * @return
     */
    public static Collection<String> parseString(String text, String interval) {
        if (text == null) return new ArrayList<>();
        String[] arrays = text.split(interval);
        return Arrays.asList(arrays);
    }
}
